<?php

namespace App\Modules\ImportDorisData\Factory\DorisTables;

use App\Common\Librarys\Str;
use App\Modules\ImportDorisData\Factory\BaseTable;
use Swoole\Database\PDOPool;

class CallTaskTable extends BaseTable
{
    /**
     * @var
     */
    const TYPE = 'call_task';

    /**
     * @var string[]
     */
    protected $fields = ['company_id', 'task_id', 'task_type', 'project_id', 'task_name', 'project_name', 'pv'];

    public function importData(PDOPool $pool): bool
    {
        try {
            $pdo = $pool->get();
            $sql = 'insert into call_task(' . implode(',', $this->fields) .') values(:'. implode(',:', $this->fields) .')';
            $p = $pdo->prepare($sql);
            for ($i = $this->start; $i <= $this->genDataLines; $i++) {
                $importData = [
                    'company_id' => 10000,
                    'task_id' => $i % 1000,
                    'task_type' => 1,
                    'project_id' => $i % 1000,
                    'task_name' => Str::randomPhone(),
                    'project_name' => '测试doris导入',
                    'pv' => 1,
                ];

                foreach ($importData as $key => $value) {
                    $p->bindParam(':' . $key, $value);
                    unset($value);
                }
                $p->execute();
                sleep(5);
            }
        } catch (\Exception $exception) {
            echo "<pre>";
            print_r($p->debugDumpParams());
            echo $exception->getMessage();
        } finally {
            $pool->put($pdo);
        }
        return true;
    }
}